package com.maven.front.service.frontSubject;

import com.maven.front.bean.*;
import com.maven.front.dto.EduCommentDto;
import com.maven.front.mapper.*;
import com.maven.front.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Service
@Slf4j
public class SubjectService {

    @Resource
    private SysSubjectMapper sysSubjectMapper;

    @Resource
    private EduCourseMapper eduCourseMapper;

    @Resource
    private EduTeacherMapper eduTeacherMapper;

    @Resource
    private EduCourseTeacherMapper eduCourseTeacherMapper;

    @Resource
    private EduCourseKpointMapper eduCourseKpointMapper;

    @Resource
    private EduCommentMapper eduCommentMapper;

    @Resource
    private EduUserMapper eduUserMapper;
    /**
     * 查询所有开班类型
     * @return
     */
    public List<SysSubject> getSubjects(){
        return sysSubjectMapper.select();
    }

    /**
     * 查询某科目下所有课程
     * @param subjectId 科目ID
     * @return
     */
    public List<EduCourse> getEduCourseBySubjectId(Integer subjectId){
        return eduCourseMapper.getBySubjectId(subjectId);
    }

    /**
     * 查询所有教师
     * @return
     */
    public List<EduTeacher> getTeachers(){
        return eduTeacherMapper.getList();
    }

    /**
     * 根据科目 或教师 查询课程
     * @param subjectId 科目Id
     * @param id 教师Id
     * @parm  sortType 排序规则
     * @return
     */
    public List<EduCourse> getEduCourseByTeacherId(Integer subjectId,Integer id){
        List<Integer> list = eduCourseTeacherMapper.selectByTeacherId(id);
        List<EduCourse> courses = new ArrayList<>();
        list.forEach((i)->courses.add(eduCourseMapper.selectByPrimaryKey(i)));
//        list交集
//        List<EduCourse> courses2 = eduCourseMapper.getBySubjectId(subjectId);
//        courses2.retainAll(courses);
        //return courses2;
        return courses;
    }

    /**
     *  查询该课程下所有章节
     * @param courseId 课程ID
     * @return
     */
    public List<EduCourseKpoint>  getKpointListByCourseId(Integer courseId){
        return  eduCourseKpointMapper.selectEduCourse(courseId);
    }

    /**
     *  根据课程ID 返回授课教师信息
     * @param courseId 课程ID
     * @return
     */
    public List<EduTeacher> getTeachersByCourseId(Integer courseId){
        List<Integer> list = eduCourseTeacherMapper.selectByCourseId(courseId);
        log.debug("*********共有"+list.size()+"名教师教授该课程");
        List<EduTeacher> teachers = new ArrayList<>();
        for(Integer i:list){
            EduTeacher eduTeacher = eduTeacherMapper.selectByPrimaryKey(i);
            teachers.add(eduTeacher);
        }
        return teachers;
    }

    /**
     *  根据课程Id 查询该课程下所有评论及回复
     * @param courseId
     * @return
     */
    public List<EduCommentDto> getCommentsByCourseId(Integer courseId){
        List<EduCommentDto> list = new ArrayList<>();
        List<EduComment> comments = eduCommentMapper.selectByOtherId(courseId);
        Collections.sort(comments, new Comparator<EduComment>() {
            @Override
            public int compare(EduComment o1, EduComment o2) {
                return o2.getAddtime().compareTo(o1.getAddtime());
            }
        });
        List<EduUser> users = eduUserMapper.select(null);
        for(EduComment c:comments){
            EduCommentDto dto = new EduCommentDto();
            BeanUtils.copy(c,dto);
            for(EduUser u:users){
                if(c.getUserId()==u.getUserId()){
                    dto.setShowName(u.getShowName());
                    dto.setPicImg(u.getPicImg());
                    break;
                }
            }
            list.add(dto);
        }
        return list;
    }

    public List<EduCourse> getEduCourses(EduCourse eduCourse){
        return eduCourseMapper.select(eduCourse);
    }


}
